<?php

/* OpenBIBLIO -- A library administration web interface
 * Copyright (C) 2011-2012 Laurent Luyssen   <big.lol13@gmail.com>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */

require_once "db.inc";
require_once 'searchDAO.inc';

class ColCfg {
	public $id = -1;
	public $priority = 0;
	public $db_name = "";
	
	public function __construct($priority, $db_name, $id = -1) {
		$this->priority = $priority;
		$this->db_name  = $db_name;
		$this->id       = $id;
	}
}

class ColCfgDAO {
	public $admin_name = "";
	public $page = "";
	private $cols = array();
	private $max_prio = 0;
	private static $default_cfg = FALSE;
			
	public function __construct($admin_name, $page) {
		$this->admin_name = $admin_name;
		$this->page = $page;
		if (!self::$default_cfg)
			self::init();
	}
	
	private static function init() {
		self::$default_cfg = array(
			"search" => array(
				new ColCfg(0, 'numero'),
				new ColCfg(1, 'nom'),
				new ColCfg(2, 'auteur'),
				new ColCfg(3, 'genre'),
				new ColCfg(4, 'collection'),
				new ColCfg(5, 'edition'),
				new ColCfg(6, 'etat'),
				new ColCfg(7, 'location_ID')
			),
			"readers" => array(
				new ColCfg(0, 'numero'),
				new ColCfg(1, 'nom'),
				new ColCfg(2, 'auteur'),
				new ColCfg(3, 'genre'),
				new ColCfg(4, 'collection'),
				new ColCfg(5, 'edition'),
				new ColCfg(6, 'etat'),
				new ColCfg(7, 'location_ID')
			)
		);
	}
	
	public static function getDefaultConfig($page = "") {
		if (!self::$default_cfg)
			self::init();
		if ($page == "")
			return self::$default_cfg["search"];
		else
			return self::$default_cfg[$page];
	}
	
	public function add(ColCfg $col) {
		if (SearchDAO::isAllowedCol($col->db_name)) {
			$col->priority = $this->max_prio;
			$this->max_prio++;
			$this->cols[] = $col;
			return TRUE;
		}
		else
			return FALSE;
	}
	
	public function moveUp($id) {
		$nb = count($this->cols);
		for ($i = 1; $i < $nb; $i++) {
			if ($id == $this->cols[$i]->id) {
				$tmp = $this->cols[$i -1];
				$this->cols[$i -1] = $this->cols[$i];
				$this->cols[$i] = $tmp;
				$tmp = $this->cols[$i -1]->priority;
				$this->cols[$i -1]->priority = $this->cols[$i]->priority;
				$this->cols[$i]->priority = $tmp;
				$i = $nb;
			}
		}
	}
	
	public function moveDown($id) {
		$nb = count($this->cols);
		for ($i = $nb - 1; $i > 0; $i--) {
			if ($id == $this->cols[$i - 1]->id) {
				$tmp = $this->cols[$i -1];
				$this->cols[$i -1] = $this->cols[$i];
				$this->cols[$i] = $tmp;
				$tmp = $this->cols[$i -1]->priority;
				$this->cols[$i -1]->priority = $this->cols[$i]->priority;
				$this->cols[$i]->priority = $tmp;
				$i = 0;
			}
		}
	}

	public function delete($id) {
		$cols = $this->cols;
		$this->cols = array();
		foreach($cols as $col)
			if ($id != $col->id)
				$this->cols[] = $col;
	}

	public function getConfig() {
		if (count($this->cols) > 0)
			return $this->cols;
		
		$sql = "SELECT c.id, c.db_col, c.priority 
			FROM cfg_columns c
			JOIN admin a ON a.id = c.id_admin
			WHERE a.login = '".$this->admin_name."' 
			AND c.page='".$this->page."' 
			ORDER BY c.priority ASC";
		$res = biblio_db_query($sql);
		if (($res === FALSE) || (biblio_db_num_rows($res) < 1))
			$this->cols = self::getDefaultConfig($this->page);
		else {
			$this->cols = array();
			while ($row = biblio_db_fetch_array($res)) {
				$this->cols[] = new ColCfg($row['priority'], $row['db_col'], $row['id']);
				$this->max_prio = $row['priority'] + 1;
			}
		}
		return $this->cols;
	}
	
	public function setConfig() {
		// Get user ID
		$sql = "SELECT id FROM admin WHERE login = '".$this->admin_name."'";
		$res = biblio_db_query ($sql);
		if ($res === FALSE)
			return FALSE;
		$user = biblio_db_fetch_object ($res);
		$id_admin = $user->id;
		
		// Delete previous config 
		biblio_db_query("DELETE FROM cfg_columns WHERE id_admin=$id_admin and page='".$this->page."'");
		
		// Write new config
		if (count($this->cols) > 0) {
			$sql = "INSERT INTO cfg_columns(id_admin, db_col, priority, page) VALUES ";
			$nb_cols = count($this->cols);
			for ($i =0; $i < $nb_cols; $i++) {
				$col = $this->cols[$i];
				$values = "($id_admin, '".$col->db_name."', ".$col->priority.",'".$this->page."')";
				$res = biblio_db_query($sql.$values);
				if ($res !== FALSE)
					$this->cols[$i]->id = biblio_db_insert_id();
				else 
					return FALSE;
			}
		}
		return TRUE;
	}
}
?>
